package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Goods;
import cn.edu.sig.bean.Message;
import cn.edu.sig.bean.Style;
import cn.edu.sig.bean.User;
import cn.edu.sig.dao.MessageDao;
import cn.edu.sig.util.PaginationHelper;
@Repository("messageDao")
public class MessageDaoImpl extends TemplateSupport implements MessageDao {

	public void add(Message message) {

		String sql = this.sqlSource.getSql("message.add");

		Object[] param = new Object[] { message.getTitle(),
				message.getSummary(), message.getCreateTime(),message.getGoods().getGoodsId(),
				message.getUser().getCode() };

		this.getJdbcTemplate().update(sql, param);

	}

	public void delete(Integer goodsId) {
		String sql = this.sqlSource.getSql("message.delete");

		this.getJdbcTemplate().update(sql, goodsId);

	}

	public void edit(Message message) {

		String sql = this.sqlSource.getSql("message.add");

		Object[] param = new Object[] { message.getTitle(),
				message.getSummary(), message.getStatus(),
				message.getMessageId() };

		this.getJdbcTemplate().update(sql, param);

	}

	public List<Message> findAll() {
		String sql = this.sqlSource.getSql("message.list");

		RowMapper<Message> mapper = new RowMapper<Message>() {
			public Message mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				Goods goods = new Goods();
				user.setCode(rs.getString("code"));
				goods.setGoodsId(rs.getInt("goodsId"));
				Message message = new Message(goods, user, rs
						.getString("title"), rs.getString("summary"), rs
						.getDate("createTime"), rs.getInt("status"));
				message.setMessageId(rs.getInt("messageId"));
				

				return message;
			}
		};

		return this.getJdbcTemplate().query(sql, mapper);
	}

	public Message findById(String messageId) {
		return null;
	}

	public Message findByName(String name) {
		return null;
	}

	public CurrentPage<Message> findAll(int pageNo, int pageSize) {
		
		PaginationHelper<Message> ph = new PaginationHelper<Message>();
		
		String sqlCountRows = "select count(*) from message";
		String sqlFetchRows = "SELECT messageId,title,summary,g.* FROM message m left join goods g on m.goodsId = g.goodsId limit ? ,?";
		
		return ph.fetchPage(getJdbcTemplate(), sqlCountRows, sqlFetchRows, "", pageNo, pageSize, new ParameterizedRowMapper<Message>(){

			public Message mapRow(ResultSet rs, int arg1) throws SQLException {
				User user = new User();
				Goods goods = new Goods();
				user.setCode(rs.getString("code"));
				goods.setGoodsId(rs.getInt("goodsId"));
				goods.setGoodsName(rs.getString("goodsName"));
				goods.setChangeType(rs.getString("changeType"));
				goods.setCreateTime(rs.getDate("createTime"));
				goods.setPublicTime(rs.getDate("publicTime"));
				Message message = new Message(goods, user, rs
						.getString("title"), rs.getString("summary"), rs
						.getDate("createTime"), rs.getInt("status"));
				message.setMessageId(rs.getInt("messageId"));
				

				return message;
			}
			
		});
	
	}

}
